package com.zhou.code;


import com.zhou.code.model.MkJSONObject;
import com.zhou.code.template.ColumnRule;
import com.zhou.code.template.Param;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;

public class ColumnUtil {

    public static String formartHumpParam(String param, ColumnRule rule) {
        switch (rule) {
            case REAL_NAME:
                return param;
            case UNDERLINE:
                return param;
            case LOWERCASE_FIRST_HUMP:
                return lowercaseFirstLetter(underlineToHump(param));
            case CAPITALIZED_FIRST_HUMP:
                return underlineToHump(param);
            case CAPITALIZED_UNDERLINE:
                return capitalizeUnderline(param);
        }
        return null;
    }

    public static String capitalizeUnderline(String column) {
        return column.toUpperCase();
    }

    /**
     * 下划线转驼峰,首字母大写
     */
    public static String underlineToHump(String column) {
        String[] strs = column.split("_");
        StringBuilder builder = new StringBuilder();
        Arrays.stream(strs)
                .map(ColumnUtil::capitalizeFirstLetter)
                .forEach(builder::append);
        return capitalizeFirstLetter(builder.toString());
    }

    /**
     * 首字母大写
     */
    public static String capitalizeFirstLetter(String column) {
        return column.substring(0, 1).toUpperCase() + column.substring(1);
    }

    /**
     * 首字母小写
     */
    public static String lowercaseFirstLetter(String column) {
        return column.substring(0, 1).toLowerCase() + column.substring(1);
    }

    /**
     * 将一个字段描述信息转换为java的class类型
     */
    public static Class dataTypeToClass(String dataType) {
        DataTypeEnum dataTypeEnum = DataTypeEnum.getByName(dataType);
        switch (dataTypeEnum) {
            case BLOB:
            case LONGBLOB:
                return byte[].class;
            case DECIMAL:
                return BigDecimal.class;
            case DOUBLE:
                return Double.class;
            case FLOUT:
                return Float.class;
            case INT:
            case SMALLINT:
                return Integer.class;
            case BIGINT:
                return Long.class;
            case DATE:
            case DATETIME:
            case TIMESTAMP:
                return Date.class;
            case TINYINT:
                return Boolean.class;
            case TEXT:
            case CHAR:
            case LONGTEXT:
            case VARCHAR:
                return String.class;
            case JSON:
                return MkJSONObject.class;
            default :
                throw new RuntimeException("请在异常抛出这里，来添加你所需要的类型");
        }
    }

    /**
     * 将一个字段描述信息转换为java的class类型
     */
    public static String dataTypeToJdbcType(String dataType) {
        DataTypeEnum dataTypeEnum = DataTypeEnum.getByName(dataType);
        switch (dataTypeEnum) {
            case TEXT:
                return "LONGVARCHAR";
            case INT:
                return "INTEGER";
            default :
                return dataTypeEnum.name;
        }
    }

    /**
     * 获取一个类的名称（不支持内部类）
     */
    public static String getRealName(Class tclass) {
        String[] strs = tclass.toString().split("\\.");
        return strs[strs.length - 1];
    }

    enum DataTypeEnum {
        BIGINT("bigint"),
        BLOB("blob"),
        CHAR("char"),
        DATE("date"),
        DATETIME("datetime"),
        DECIMAL("decimal"),
        DOUBLE("double"),
        FLOUT("float"),
        INT("int"),
        JSON("json"),
        LONGBLOB("longblob"),
        LONGTEXT("longtext"),
        SMALLINT("smallint"),
        TEXT("text"),
        TIMESTAMP("timestamp"),
        TINYINT("tinyint"),
        VARCHAR("varchar");

        private String name;

        DataTypeEnum(String name) {
            this.name = name;
        }

        public static DataTypeEnum getByName(String name) {
            for (DataTypeEnum value : values()) {
                if (value.getName().equals(name)) {
                    return value;
                }
            }
            throw new RuntimeException("不被支持的类型：" + name);
        }

        public String getName() {
            return name;
        }
    }
}
